
//
//  QKKVOTestCtrl.m
//  testTool
//
//  Created by Su on 2018/12/17.
//  Copyright © 2018 com.qiankun. All rights reserved.
//

#import "QKKVOTestCtrl.h"
#import "QKTestObject.h"
#import "QKTestObserver.h"

@interface QKKVOTestCtrl ()

@end

@implementation QKKVOTestCtrl

-(void)injected{
    [self viewDidLoad];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    
    QKTestObject *obj = [[QKTestObject alloc] init];
    QKTestObserver *observer = QKTestObserver.alloc.init;
    
//    通过 KVO 监听 obj 的变化
    
    [obj addObserver:observer forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:NULL];
    
//    通过 setter 修改 value, 监听生效
//    obj.value = 2;
    
//    通过 kvo 修改 value , 监听生效
//    [obj setValue:@4 forKey:@"value"];
    
    
//    通过成员变量赋值. 未执行监听方法中的输出
    /*
        成员变量需要手动 添加 KVO , 监听才会生效
         [self willChangeValueForKey:@"value"];
         _value += 1;
         [self didChangeValueForKey:@"value"];
     */
    [obj changeValue];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
